package com.xc.freeapp.util;

import com.alibaba.fastjson.JSONArray;

import org.apache.log4j.Logger;
import org.sword.wechat4j.token.TokenProxy;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class UploadUtil {

	private static Logger log = Logger.getLogger(UploadUtil.class.getClass());
	private static String FILEURL = "http://" + QiniuUpload.getQn_domain()
			+ "/";
	

	public static void main(String[] args) {
		// String
		// imageURLs="[\"vaccineperson_picture/2015111914275929927.jpg\",\"vaccineperson_picture/2015111914275929927.jpg\"]";
		// List<String> picslist = JsonUtil.parseList(imageURLs, String.class);
		// System.out.println(picslist);

	}

	public static void delete(String pictureBuffer) {
		if (pictureBuffer.startsWith(FILEURL)) {
			pictureBuffer = pictureBuffer.replaceFirst(FILEURL, "");
		}
		QiniuUpload.delete(pictureBuffer);
	}

	public static void deletes(List<String> pics) {
		for (String string : pics) {
			delete(string);
		}
	}

	/**
	 * 单张图片上传
	 * 
	 * @param pictureBuffer
	 * @return
	 */
	public static String upload(String pictureBuffer, Integer appId) {
		
		
		
		if (pictureBuffer == null || "".equals(pictureBuffer)) {
			return "";
		}
		String imageurl = "";
		if (pictureBuffer.startsWith("http://")) {
			String [] path = pictureBuffer.split("/");
			return path[path.length-1];
		}
		if (pictureBuffer.length() >= 50) {
			try {
				imageurl = QiniuUpload.upload(pictureBuffer,
						QiniuUpload.getUpToken0(), appId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (pictureBuffer.length() < 100) {
			Random random = new Random();
			File file = new File("weixinupload");
			if (!file.exists() && !file.isDirectory()) {
				System.out.println("//不存在");
				file.mkdir();
			} else {
				System.out.println("//目录存在");
			}
			String filename = "weixinupload/" + (new Date().getTime()) + ""
					+ random.nextInt(1000) + ".png";
			String urlString = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="
					+ TokenProxy.accessToken() + "&media_id=" + pictureBuffer;
			log.debug("微信下载图片地址" + urlString);
			try {
				DownloadByURL.download(urlString, filename);
			} catch (Exception e) {
				log.error("从微信下载图片出错" + e.getMessage());
			}

			try {
				imageurl = QiniuUpload.upload(new File(filename),
						QiniuUpload.getUpToken0(), appId);
				log.debug("imageurl=" + imageurl);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error("上传图片到七牛出错" + e.getMessage());
			}
			File weixinfile = new File(filename);
			// if(weixinfile.exists()){
			// weixinfile.delete();
			// }
		}
		return imageurl;
	}

	/**
	 * 多图片上传
	 * @param pics
	 * @param appId
	 * @return
   */
	public static String uploads(List<String> pics, Integer appId) {
		if (pics == null || "".equals(pics)) {
			return "";
		}
		List<String> picspath = new ArrayList<String>();
		for (String pic : pics) {
			picspath.add(upload(pic, appId));
		}
		return JSONArray.toJSONString(picspath);

	}

	/**
	 * 多图片读取路径转为list
	 * 
	 * @param imageURLs
	 * @return
	 */
	public static List<String> pathstoList(String imageURLs) {
		if (imageURLs == null || "".equals(imageURLs)) {
			return null;
		}
		List<String> picslist = JSONArray.parseArray(imageURLs, String.class);
		for (int i = 0; i < picslist.size(); i++) {
			if (picslist.get(i) != null && !picslist.get(i).startsWith("http")) {
				picslist.set(i, FILEURL + picslist.get(i));
			}

		}
		return picslist;
	}

	/**
	 * 单图片读取url转换
	 * 
	 * @param url
	 * @return
	 */
	public static String urlConversion(String url) {
		if (url == null || "".equals(url)) {
			return "";
		}
		if (url.startsWith("http")) {
			return url;
		}
		return FILEURL + url;
	}
}
